package com.ymatou.iapi.coupon.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.isReceivedCouponByIdempotentKeyBean;
import com.ymatou.iapi.coupon.parameter.receiveCouponBean;
import com.ymatou.iapi.coupon.service.isReceivedCouponByIdempotentKeyCall;
import com.ymatou.iapi.coupon.service.receiveCouponCall;
import com.ymatou.iapi.couponmanage.parameter.CreateCouponBean;
import com.ymatou.iapi.couponmanage.service.CreateCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.utils.TestDataManager;
/**
*幂等键是否领取了优惠券
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_isReceivedCouponByIdempotentKey{
	private static isReceivedCouponByIdempotentKeyBean bean;
	private static isReceivedCouponByIdempotentKeyCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("幂等键是否领取了优惠券");
	}

	@Before
	public void caseUp() {
		bean=new isReceivedCouponByIdempotentKeyBean();
		call=new isReceivedCouponByIdempotentKeyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_isReceivedCouponByIdempotentKey_001() {
		Logger.start(true,"正例-幂等键是否领取了优惠券-领取优惠券");
		try {
			receiveCouponBean rBean=new receiveCouponBean();
			receiveCouponCall rCall=new receiveCouponCall();
			int userId=Integer.parseInt(TestDataManager.getRandomNum(5));
			CouponServiceMCallServiceV2 CouponServiceMCallServiceV2 = new CouponServiceMCallServiceV2();
			CreateCouponBean createcouponBean = CouponServiceMCallServiceV2.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall = CouponServiceMCallServiceV2.createPlatformCoupon(createcouponBean);
            List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());	
			
			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> batchIdlist = new ArrayList<>();
			for (String batchCode:batchCodes) {
				couponbatch batch = new couponbatchWapper().selectBybatchCode(batchCode).get(0);
				batchIdlist.add(batch.getBatchId());
				receiveCountlist.add(batch.getReceivedCount());
			}
			
			rBean.setIdentityKey(TestDataManager.getRandomUUID().toString());
			rBean.setUserId(userId);
			rBean.setUserType(0);
			rBean.setBatchCodes(batchCodes);

			rCall.setData(rBean);
			rCall.callService();		
			
			bean.setIdempotentKey(rBean.getIdentityKey());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals(true, call.getReceived(), "验证received");
			Logger.verifyEquals("0", call.getErrorCode(), "验证ErrorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_isReceivedCouponByIdempotentKey_001_1() {
		Logger.start(true,"正例-幂等键是否领取了优惠券-未领取优惠券");
		try {

			bean.setIdempotentKey(TestDataManager.getRandomUUID().toString());
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals(false, call.getReceived(), "验证received");
			Logger.verifyEquals("0", call.getErrorCode(), "验证ErrorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_isReceivedCouponByIdempotentKey_002() {
		Logger.start(true,"正例-幂等键是否领取了优惠券-idempotentKey为空-返回参数错误");
		try {
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals(false, call.getReceived(), "验证received");
			Logger.verifyEquals("1", call.getErrorCode(), "验证ErrorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}